﻿using JBeamLibrary.Tools;
using JBeamLibrary.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.VehicleParts.Interfaces;
using Newtonsoft.Json.Linq;

namespace JBeamLibrary.VehicleParts.Elements
{
    public class Prop : VehicleElement
    {
        #region Properties

        public string Func { get; set; }

        public string Mesh { get; set; }

        public string NodeIdRef { get; set; }

        public string NodeIdX { get; set; }

        public string NodeIdY { get; set; }

        public Float3 BaseRotation { get; set; }

        public Float3 Rotation { get; set; }

        public Float3 Translation { get; set; }

        public float Min { get; set; }

        public float Max { get; set; }

        public float Offset { get; set; }

        public float Multiplier { get; set; }

        public bool IsLight
        {
            get { return Mesh.SafeEquals("SPOTLIGHT"); }
            set
            {
                Mesh = value ? "SPOTLIGHT" : string.Empty;
            }
        }

        #endregion

        #region CTORs

        public Prop()
        {
            BaseRotation = Float3.Invalid;
            Rotation = Float3.Invalid;
            Translation = Float3.Invalid;
            Min = float.MinValue;
            Max = float.MinValue;
            Offset = float.MinValue;
            Multiplier = float.MinValue;
        }

        public Prop(string func, string mesh, string idRef, string idX, string idY)
        {
            Func = func;
            Mesh = mesh;
            NodeIdRef = idRef;
            NodeIdX = idX;
            NodeIdY = idY;
        }

        #endregion

        #region Methods

        public override IVehicleElement FromJBeam(JToken token)
        {
            var arr = token as JArray;
            if (arr == null) return this;

            Func = arr[0].FromJBeam<string>();
            Mesh = arr[1].FromJBeam<string>();
            NodeIdRef = arr[2].FromJBeam<string>();
            NodeIdX = arr[3].FromJBeam<string>();
            NodeIdY = arr[4].FromJBeam<string>();
            BaseRotation = arr[5].FromJBeam<Float3>();
            Rotation = arr[6].FromJBeam<Float3>();
            Translation = arr[7].FromJBeam<Float3>();
            Min = arr[8].FromJBeam<float>();
            Max = arr[9].FromJBeam<float>();
            Offset = arr[10].FromJBeam<float>();
            Multiplier = arr[11].FromJBeam<float>();

            return this;
        }

        public override JToken ToJBeam()
        {
            return new JArray
            {
                Func,
                Mesh,
                NodeIdRef,
                NodeIdX,
                NodeIdY,
                BaseRotation.ToJBeam(),
                Rotation.ToJBeam(),
                Translation.ToJBeam(),
                Min,
                Max,
                Offset,
                Multiplier
            };
        }

        public override string ToString()
        {
            return string.Format("{0} > {1}", Func, Mesh);
        }

        #endregion
    }
}
